# Maintainer: Santiago Torres-Arias <santiago @ usualplace>
# Contributor: Kaizhao Zhang <zhangkaizhao@gmail.com>
# Contributor: Phillip A. (flying-sheep)

pkgname=python-hatch
pkgver=1.7.0
pkgrel=4
pkgdesc="A modern project, package, and virtual env manager"
arch=('any')
url="https://github.com/pypa/hatch"
license=('MIT')
depends=(
  'python'
  'python-click'
  'python-hatchling'
  'python-httpx'
  'python-hyperlink'
  'python-keyring'
  'python-packaging'
  'python-pexpect'
  'python-platformdirs'
  'python-rich'
  'python-shellingham'
  'python-tomli-w'
  'python-tomlkit'
  'python-userpath'
  'python-virtualenv'
)
makedepends=(
  'python-build'
  'python-hatch-vcs'
  'python-installer'
  'python-wheel'
)
checkdepends=(
  'git'
  'python-pytest'
)
_name=${pkgname/python-/}
source=("$url/archive/refs/tags/$_name-v$pkgver.tar.gz")
sha256sums=('0a71d96fc008e7c6f6ba01324a0362cb2098f04b515a0dc47644ec22ea431a71')
b2sums=('7ef4df3c724000c05783ee83dd3759096107eccb56b9d99bf1463daf82bd0cd24f015d85ef34f7b898adb3f4b77c17f6840ffda4f70ee5baeb44619b4055c80f')

build() {
  cd $_name-$_name-v$pkgver
  python -m build --wheel --no-isolation
}

check() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  local pytest_options=(
    -vv
    --deselect tests/backend/builders/test_custom.py::test_default
    --deselect tests/backend/builders/test_custom.py::test_explicit_path
    --deselect tests/backend/builders/test_sdist.py::TestBuildStandard::test_no_strict_naming
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_default_auto_detection
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_default
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_default_extra_dependencies
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_default_force_include
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_default_force_include_option
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_exact
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_exact_extra_dependencies
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_exact_force_include
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_exact_force_include_option
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_exact_force_include_build_data_precedence
    --deselect tests/backend/builders/test_wheel.py::TestBuildStandard::test_editable_pth
    --deselect tests/cli/config/test_set.py::test_project_location_basic_set_first_project
    --deselect tests/cli/config/test_set.py::test_project_location_complex_set_first_project
    --deselect tests/cli/build/test_build.py::test_backend_not_build_system
    --deselect tests/cli/build/test_build.py::test_backend_not_build_dependency
    --deselect tests/cli/build/test_build.py::test_incompatible_environment
    --deselect tests/cli/build/test_build.py::test_unknown_targets
    --deselect tests/cli/build/test_build.py::test_mutually_exclusive_hook_options
    --deselect tests/cli/build/test_build.py::test_default
    --deselect tests/cli/build/test_build.py::test_explicit_targets
    --deselect tests/cli/build/test_build.py::test_explicit_directory
    --deselect tests/cli/build/test_build.py::test_explicit_directory_env_var
    --deselect tests/cli/build/test_build.py::test_clean
    --deselect tests/cli/build/test_build.py::test_clean_env_var
    --deselect tests/cli/build/test_build.py::test_clean_only
    --deselect tests/cli/build/test_build.py::test_clean_only_hooks_only
    --deselect tests/cli/build/test_build.py::test_clean_hooks_after
    --deselect tests/cli/build/test_build.py::test_clean_hooks_after_env_var
    --deselect tests/cli/build/test_build.py::test_clean_only_no_hooks
    --deselect tests/cli/build/test_build.py::test_hooks_only
    --deselect tests/cli/build/test_build.py::test_hooks_only_env_var
    --deselect tests/cli/build/test_build.py::test_extensions_only
    --deselect tests/cli/build/test_build.py::test_no_hooks
    --deselect tests/cli/build/test_build.py::test_no_hooks_env_var
    --deselect tests/cli/build/test_build.py::test_debug_verbosity
    --deselect tests/cli/build/test_build.py::test_shipped
    --deselect tests/cli/build/test_build.py::test_build_dependencies
    --deselect tests/cli/build/test_build.py::test_plugin_dependencies_unmet
    --deselect tests/cli/clean/test_clean.py::test
    --deselect tests/cli/config/test_explore.py::test_call
    --deselect tests/cli/config/test_find.py::test_copy
    --deselect tests/cli/dep/test_hash.py::test_plugin_dependencies_unmet
    --deselect tests/cli/dep/show/test_requirements.py::test_plugin_dependencies_unmet
    --deselect tests/cli/dep/show/test_table.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_create.py::test_new_selected_python
    --deselect tests/cli/env/test_create.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_create.py::test_plugin_dependencies_met
    --deselect tests/cli/env/test_find.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_prune.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_remove.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_run.py::test_plugin_dependencies_unmet
    --deselect tests/cli/env/test_show.py::test_plugin_dependencies_unmet
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesInstalled::test_default_all
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesInstalled::test_field_readme
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesInstalled::test_field_string
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesInstalled::test_field_complex
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_incompatible_environment
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_default_all
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_field_readme
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_field_string
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_field_complex
    --deselect tests/cli/project/test_metadata.py::TestBuildDependenciesMissing::test_plugin_dependencies_unmet
    --deselect tests/cli/publish/test_publish.py::test_timestamp_to_version
    --deselect tests/cli/publish/test_publish.py::test_explicit_options
    --deselect tests/cli/publish/test_publish.py::test_unknown_publisher
    --deselect tests/cli/publish/test_publish.py::test_disabled
    --deselect tests/cli/publish/test_publish.py::test_repo_invalid_type
    --deselect tests/cli/publish/test_publish.py::test_repo_missing_url
    --deselect tests/cli/publish/test_publish.py::test_missing_user
    --deselect tests/cli/publish/test_publish.py::test_missing_auth
    --deselect tests/cli/publish/test_publish.py::test_flags
    --deselect tests/cli/publish/test_publish.py::test_plugin_config
    --deselect tests/cli/publish/test_publish.py::test_plugin_config_repo_override
    --deselect tests/cli/publish/test_publish.py::test_prompt
    --deselect tests/cli/publish/test_publish.py::test_initialize_auth
    --deselect tests/cli/publish/test_publish.py::test_external_artifact_path
    --deselect tests/cli/publish/test_publish.py::test_already_exists
    --deselect tests/cli/publish/test_publish.py::test_no_artifacts
    --deselect tests/cli/publish/test_publish.py::test_enable_with_flag
    --deselect tests/cli/publish/test_publish.py::test_enable_with_prompt
    --deselect tests/cli/publish/test_publish.py::TestWheel::test_missing_required_metadata_field[name]
    --deselect tests/cli/publish/test_publish.py::TestWheel::test_missing_required_metadata_field[version]
    --deselect tests/cli/publish/test_publish.py::TestSourceDistribution::test_missing_required_metadata_field[name]
    --deselect tests/cli/publish/test_publish.py::TestSourceDistribution::test_missing_required_metadata_field[version]
    --deselect tests/cli/run/test_run.py::test_plugin_dependencies_unmet
    --deselect tests/cli/version/test_version.py::test_show_dynamic_missing_build_dependencies
    --deselect tests/cli/version/test_version.py::test_plugin_dependencies_unmet
    --deselect tests/index/test_core.py::TestTLS::test_default
    --deselect tests/index/test_core.py::TestTLS::test_ca_cert
    --deselect tests/index/test_core.py::TestTLS::test_client_cert
    --deselect tests/index/test_core.py::TestTLS::test_client_cert_with_key
  )

  cd $_name-$_name-v$pkgver
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  pytest "${pytest_options[@]}"
}

package() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $_name-$_name-v$pkgver
  python -m installer --destdir="${pkgdir}" dist/*.whl
  install -Dm644 LICENSE.txt -t "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  export PYTHONPATH="$pkgdir/$site_packages:$PYTHONPATH"
  export PATH="$pkgdir/usr/bin:$PATH"
  _HATCH_COMPLETE=bash_source "$_name" | install -vDm 644 /dev/stdin "$pkgdir/usr/share/bash-completion/completions/$_name"
  _HATCH_COMPLETE=fish_source "$_name" | install -vDm 644 /dev/stdin "$pkgdir/usr/share/fish/vendor_completions.d/$_name.fish"
  _HATCH_COMPLETE=zsh_source "$_name" | install -vDm 644 /dev/stdin "$pkgdir/usr/share/zsh/site-functions/_$_name"
}
